package com.itheima.d7_thread_pool;

import java.util.concurrent.*;

/**
 * 实现Runnable接口
 */
public class ThreadPoolTest1 {
    public static void main(String[] args) {
        //1.通过ThreadPoolExecutor创建一个线程池对象
//            public ThreadPoolExecutor(int corePoolSize,
//        int maximumPoolSize,
//        long keepAliveTime,
//        TimeUnit unit,
//        BlockingQueue<Runnable> workQueue,
//        ThreadFactory threadFactory,
//        RejectedExecutionHandler handler)
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        Runnable target = new MyRunnable();
        pool.execute(target);//自动创建一个新的线程
        pool.execute(target);//自动创建一个新的线程
        pool.execute(target);//自动创建一个新的线程
        //任务队列  new ArrayBlockingQueue<>(4),
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);//复用前面的核心线程

        //临时2个
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);//复用前面的核心线程
        pool.execute(target);//复用前面的核心线程


        //关闭线程池
        pool.shutdown();
//        pool.shutdownNow();立即


    }
}
